//index.js
//获取应用实例
var app = getApp();
var _openid = app.getOpenId();
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    parkingInfo: [],
    parkingId: "",
    orderId: "",
    checked: false,
    carNumber:""
  },
  //事件处理函数
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

 //复选框的选择函数
  checkboxChange: function (e) {
      var that = this;
      var checkboxValue = !that.data.checked;
      console.log('checkbox发生change事件，携带value值为：', checkboxValue);
      that.setData({
        checked:checkboxValue
      });
  },

  carNumberInput:function(e){
    var that= this;
    that.setData({
      carNumber:e.detail.value
    })
  },


  onLoad: function (e) {
    this.setData({
      parkingId: e.id
    })
    wx.showLoading();
    var that = this;
    console.log("预定车位" + _openid);
    wx.request({
      url: app.globalData.domain + app.globalData.subDomain + '/parking/detail?',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: _openid,
        id: e.id
      },
      success: function (res) {
        var parkingInfo = [];
        wx.hideLoading();
        if(res.data.code == 100){
          if (res.data.obj.length == 0) {
            that.setData({
              loadingMoreHidden: false,
            });
            return;
          }
          parkingInfo.push(res.data.obj);
          that.setData({
            parkingInfo: parkingInfo,
          });
        }else{
          var tip = "数据获取失败," + res.data.message;
          wx.showModal({
            title: '提示',
            content: tip,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },

  bookParking: function () {
    this.modalcnt();
  },

  modalcnt: function () {
    var that = this;
    var bookParkingId = that.data.parkingId;
    var carNumber = that.data.carNumber;
    console.log(carNumber, bookParkingId);
    wx.showModal({
      title: '提示',
      content: '确认预订？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.domain + app.globalData.subDomain + '/parking/bookingParking?',
            method: 'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              openid: _openid,
              parkingID: bookParkingId,
              friendCarNumber: carNumber
            },
            success: function (res) {
              console.log(res);
              if (res.data.code == 100) {
                var orderId = res.data.obj.id;
                wx.showModal({
                  title: '提示',
                  content: '预订成功，请支付',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateTo({
                        url: "/pages/payOrderDetail/index?id=" + orderId
                      })
                    } else if (res.cancel) {
                    }
                  }
                })
              } else {
                console.log(res);
                var showCotent = res.data.message + ",暂时不能预定";
                wx.showModal({
                  title: '提示',
                  content: showCotent,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },
})
